﻿using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Http.Features;
using Utility;
using Services.IService;
using Services.Service;
using Repositories.IRepository;
using Repositories.Repository;

namespace JFnetcore
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //上传文件大小设置
            services.Configure<FormOptions>(x =>
            {
                x.ValueLengthLimit = int.MaxValue;
                x.MultipartBodyLengthLimit = int.MaxValue;
            });

            //添加数据上下文
            //SiteConfig.SiteConfigStr = Configuration.GetConnectionString("MySqllocalhost");
            SiteConfig.SiteConfigStr = Configuration.GetConnectionString("MySql");

            #region 依赖注入
            services.AddScoped<IArticleRepository, ArticleRepository>();
            services.AddScoped<IArticleService, ArticleService>();
            services.AddScoped<IArticletypeRepository, ArticletypeRepository>();
            services.AddScoped<IArticletypeService, ArticletypeService>();
            services.AddScoped<IConfigRepository, ConfigRepository>();
            services.AddScoped<IConfigService, ConfigService>();
            services.AddScoped<ILogRepository, LogRepository>();
            services.AddScoped<ILogService, LogService>();
            services.AddScoped<IMenuRepository, MenuRepository>();
            services.AddScoped<IMenuService, MenuService>();
            services.AddScoped<IProductRepository, ProductRepository>();
            services.AddScoped<IProductService, ProductService>();
            services.AddScoped<IProducttypeRepository, ProducttypeRepository>();
            services.AddScoped<IProducttypeService, ProducttypeService>();
            services.AddScoped<IReplyRepository, ReplyRepository>();
            services.AddScoped<IReplyService, ReplyService>();
            services.AddScoped<IRoleRepository, RoleRepository>();
            services.AddScoped<IRoleService, RoleService>();
            services.AddScoped<IRolemenuRepository, RolemenuRepository>();
            services.AddScoped<IRolemenuService, RolemenuService>();
            services.AddScoped<IUsersRepository, UsersRepository>();
            services.AddScoped<IUsersService, UsersService>();
            services.AddScoped<IVarticleRepository, VarticleRepository>();
            services.AddScoped<IVarticleService, VarticleService>();
            services.AddScoped<IVmenuRepository, VmenuRepository>();
            services.AddScoped<IVmenuService, VmenuService>();
            services.AddScoped<IVproductRepository, VproductRepository>();
            services.AddScoped<IVproductService, VproductService>();
            services.AddScoped<IVusersRepository, VusersRepository>();
            services.AddScoped<IVusersService, VusersService>();
            services.AddScoped<IVlogRepository, VlogRepository>();
            services.AddScoped<IVlogService, VlogService>();
            services.AddScoped<IVreplyRepository, VreplyRepository>();
            services.AddScoped<IVreplyService, VreplyService>();


            #endregion

            services.AddMvc();

            //Session服务
            services.AddSession(options => {
                //options.IdleTimeout = TimeSpan.FromHours(12);
                options.IdleTimeout = TimeSpan.FromMinutes(600);
                options.CookieName = "JinFanSugar";
            });

            services.AddDirectoryBrowser();//允许浏览目录的文件和文件夹
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();


            //允许浏览目录的文件和文件夹
            app.UseDirectoryBrowser(new DirectoryBrowserOptions()
            {
                FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"wwwroot/upload")),
                RequestPath = new Microsoft.AspNetCore.Http.PathString("/wwwroot/upload")
            });
            app.UseDirectoryBrowser(new DirectoryBrowserOptions()
            {
                FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"wwwroot/KEupload")),
                RequestPath = new Microsoft.AspNetCore.Http.PathString("/wwwroot/KEupload")
            });

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            //启用Session
            app.UseSession();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            //使用静态文件
            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Login}/{action=Index}/{id?}");
            });
        }
    }
}
